package com.zxs.controller;

import com.zxs.common.util.PageResult;
import com.zxs.common.util.R;
import com.zxs.entity.Car;
import com.zxs.service.ICangService;
import com.zxs.service.ICarService;
import com.zxs.vo.CarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-01-14
 */
@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    private ICarService carService;
    @RequestMapping("/list")
    public R getCar() {
        return R.ok().data("list",carService.list());
    }

//     分页查询
    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<Car> pr = carService.findByPage(page,pageSize);
        return R.ok().data("pr",pr);
    }
        // 条件带分页查询
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) CarVO vo){
        PageResult<Car> pr = carService.search(page,pageSize,vo);
        return R.ok().data("pr",pr);
    }

    @PostMapping("/add")
    public R add(@RequestBody Car car){
        boolean flag = carService.add(car);
        return flag ? R.ok().message("添加车辆成功!") : R.error().message("添加车辆失败!");
    }


    @PutMapping("/update")
    public R update(@RequestBody  Car car) {
        boolean flag = carService.updateById(car);
        return flag ? R.ok().message("修改车辆成功!") : R.error().message("修改车辆失败!");
    }



    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return carService.removeByIds(sids) ? R.ok().message("删除车辆成功!") : R.error().message("删除车辆失败!");
    }
}
